<template>
<div>
  <div> <img src="../../assets/back.png" width="25" height="28" @click="toMap"></div>
  <div class="category"  style="width:60%;position:relative;left:20%;">
    <!-- 按钮 -->
    
    
    <div class="btns">
      <el-button size="medium" type="primary" @click="toAdd">新增</el-button>
        
    </div>
    <!-- /按钮 -->
  
    <!-- 表格 -->
    <el-table size="small" :data="categorys">
      <el-table-column label="序号" type="index" :index="1"></el-table-column>
      <el-table-column label="分类名称" prop="name" width="160"></el-table-column>
      <el-table-column label="分类简介" prop="description"></el-table-column>
      <el-table-column label="序号" prop="no" width="60" align="center"></el-table-column>
      <el-table-column label="操作" width="100" align="center" fixed="right">
        <template v-slot:default="scope">
          <el-button @click="toEdit(scope.row)" size="medium" type="text" >修改</el-button>
          <el-button @click="toDel(scope.row)" size="medium" type="text">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- /表格 -->
   

    <!-- 模态框 -->
    <el-dialog title="录入分类信息" :visible.sync="visible">
      <el-form label-width="80px" size="small">
        <el-form-item label="名称">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="介绍">
          <el-input type="textarea" v-model="form.description"></el-input>
        </el-form-item>
        <el-form-item label="序号">
          <el-input v-model="form.no"></el-input>
        </el-form-item>
         
      </el-form>
      
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false" size="small">取 消</el-button>
        <el-button type="primary" @click="submit" size="small">确 定</el-button>
      </span>
    </el-dialog>
    <!-- /模态框 -->
  </div>
</div>
</template>

<script>
// 导入ajax工具（本质ajax -> axios -> reqeust）
import {get,post} from '@/utils/request'

export default {
  data(){
    return {
      visible:false,
      param:{
        page:1,
        pageSize:10
      },
      categorys:[],
      form:{},
      title:"录入资讯分类"
    }
  },
  created(){
    // 在vue实例创建完毕后加载分类信息
    this.loadCategoryData();
  },
  methods:{
    //跳转
    toMap(){
      this.$router.push({
        path:"/news/refute",
      })
    },
    // 提交表单
    submit(){
      let url = "/category/saveOrUpdate";
      // 修改form，不用直接操作form，先进行克隆（深拷贝）
      let data = Object.assign({},this.form);
      // 提交
      post(url,data).then(response => {
        this.$message({type:"success",message:response.message});
        this.loadCategoryData();
        this.visible = false;
      })
    },
    // 去添加
    toAdd(){
      this.title = "录入资讯分类";
      this.form = {};
      this.visible = true;
    },
    // 打开模态框准备修改
    toEdit(row){
      this.title = "修改资讯分类";
      // 将row克隆后赋值给form，避免直接修改row影响页面变化
      this.form = Object.assign({},row);
      this.visible = true;
    },
    toDel(row){
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let url = "/category/deleteById";
        get(url,{id:row.id}).then(response => {
          this.$message({type:"success",message:response.message});
          this.loadCategoryData();
        })
      })
    },
    // 加载资讯分类
    loadCategoryData(){
      let url = "/category/findAll";
      get(url).then(response => {
        this.categorys = response.data;
      });
    },
    // 监听翻页
    currentChangeHandler(page){
      this.param.page = page;
      this.loadCategoryData();
    }
  }
}
</script>